package cc.inq.es;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @Author firefly
 * @since 2018/11/29
 */
@Configuration
public class EsConfig {

    @Bean
    public TransportClient client() throws UnknownHostException {
        TransportAddress master = new TransportAddress(InetAddress.getByName("localhost"), 9300);
        TransportAddress slave_1 = new TransportAddress(InetAddress.getByName("localhost"), 8200);
        TransportAddress slave_2 = new TransportAddress(InetAddress.getByName("localhost"), 8201);

        Settings settings = Settings.builder()
                .put("cluster.name", "firefly").build();
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddresses(master, slave_1, slave_2);
        return client;
    }
}
